﻿using System.Collections.Generic;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Phone.Controls.Primitives;

namespace Stopwatch.Controls
{
    public partial class TimerControl : UserControl
    {
        const int WarningInterval = 500;

        Queue<Timer> timers = new Queue<Timer>();

        public TimerControl()
        {
            Mode = Mode.Picker;
            InitializeComponent();
            HoursLoopingSelector.IsExpandedChanged += OnSelectorIsExpandedChanged;
            MinutesLoopingSelector.IsExpandedChanged += OnSelectorIsExpandedChanged;
            SecondsLoopingSelector.IsExpandedChanged += OnSelectorIsExpandedChanged;

            (HoursLoopingSelector.DataSource as NumbersDataSource).SelectionChanged += HourSelectionChanged;
            (MinutesLoopingSelector.DataSource as NumbersDataSource).SelectionChanged += MinuteSelectionChanged;
            (SecondsLoopingSelector.DataSource as NumbersDataSource).SelectionChanged += SecondSelectionChanged;
        }

        private void OnSelectorIsExpandedChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if ((bool)e.NewValue)
            {
                var selector = sender as LoopingSelector;

                // Ensure that only one selector is expanded at a time
                HoursLoopingSelector.IsExpanded = (selector == HoursLoopingSelector);
                MinutesLoopingSelector.IsExpanded = (selector == MinutesLoopingSelector);
                SecondsLoopingSelector.IsExpanded = (selector == SecondsLoopingSelector);

                hoursTag.Visibility = HoursLoopingSelector.IsExpanded ?
                    System.Windows.Visibility.Collapsed : System.Windows.Visibility.Visible;
                minutesTag.Visibility = MinutesLoopingSelector.IsExpanded ?
                    System.Windows.Visibility.Collapsed : System.Windows.Visibility.Visible;
                secondsTag.Visibility = SecondsLoopingSelector.IsExpanded ?
                    System.Windows.Visibility.Collapsed : System.Windows.Visibility.Visible;

            }
        }

        private void HourSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.Dispatcher.BeginInvoke(() =>
            {
                Hour = int.Parse((sender as NumbersDataSource).SelectedItem.ToString());
            });
        }

        private void MinuteSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.Dispatcher.BeginInvoke(() =>
            {
                Minute = int.Parse((sender as NumbersDataSource).SelectedItem.ToString());
            });
        }

        private void SecondSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.Dispatcher.BeginInvoke(() =>
            {
                Second = int.Parse((sender as NumbersDataSource).SelectedItem.ToString());
            });
        }

        Mode mode;
        public Mode Mode
        {
            get
            {
                return mode;
            }
            set
            {
                if (mode == value)
                {
                    return;
                }

                mode = value;
                switch (mode)
                {
                    case Controls.Mode.Picker:
                        ShowPanel.Visibility = System.Windows.Visibility.Collapsed;
                        PickerPanel.Visibility = System.Windows.Visibility.Visible;
                        break;
                    case Controls.Mode.Show:
                        PickerPanel.Visibility = System.Windows.Visibility.Collapsed;
                        ShowPanel.Visibility = System.Windows.Visibility.Visible;
                        break;
                }
            }
        }

        /// <summary>
        /// show warning...
        /// </summary>
        public void Warning()
        {
            // timers.Enqueue(new Timer(WarningEffect, "false", WarningInterval, WarningInterval));
        }

        public int Hour
        {
            get
            {
                return int.Parse(HourText.Text);
            }
            set
            {
                HourText.Text = value.ToString("D2");
            }
        }

        public int Minute
        {
            get
            {
                return int.Parse(MinuteText.Text);
            }
            set
            {
                MinuteText.Text = value.ToString("D2");
            }
        }

        public int Second
        {
            get
            {
                return int.Parse(SecondText.Text);
            }
            set
            {
                SecondText.Text = value.ToString("D2");
            }
        }

        void WarningEffect(object state)
        {
            switch ((string)state)
            {
                case "false":
                    state = "true";
                    this.Dispatcher.BeginInvoke(() =>
                    {
                        foreach (var c in ShowPanel.Children)
                        {
                            c.Visibility = System.Windows.Visibility.Collapsed;
                        }
                    });
                    break;
                case "true":
                default:
                    var timer = timers.Dequeue();
                    timer.Change(Timeout.Infinite, Timeout.Infinite);
                    timer.Dispose();
                    this.Dispatcher.BeginInvoke(() =>
                    {
                        foreach (var c in ShowPanel.Children)
                        {
                            c.Visibility = System.Windows.Visibility.Visible;
                        }
                    });
                    break;
            }
        }

        public void Reset()
        {
            this.Mode = Controls.Mode.Picker;
            Hour = int.Parse(HoursLoopingSelector.DataSource.SelectedItem.ToString());
            Minute = int.Parse(MinutesLoopingSelector.DataSource.SelectedItem.ToString());
            Second = int.Parse(SecondsLoopingSelector.DataSource.SelectedItem.ToString());
        }
    }
}
